'use strict'

let models = require('../models');
let config = require('../../config');
let errors = config.errors;
let Session = require('../middlewares/session');

let Festival = models.Festival;
let School = models.School;
let Term = models.Term;

let schoolService = require('../services/school');
let teacherService = require('../services/teacher');

let actionGetAllSchools = {
    handler(req, res) {
        let schools = schoolService.getAllSchools();

        let schoolsData = School.toObjectArray(schools, {
            includes: [
                'name',
                'iflyId'
            ]
        });

        res.json({
            successful: true,
            data: {
                schools: schoolsData
            }
        });
    }
};

let actionUpdateFestivals = {
    login: true,
    privileges: [ 'leader' ],
    handler(req, res) {
        let user = req.session.login.user;
        let leader = teacherService.getTeacherByUserId(user.id);
        let school = leader.managedSchool;

        let festivalData = req.body.festivals;
        festivalData.forEach(festival => {
            delete festival.id
        });
        let festivals = schoolService.updateFestivals(school, festivalData);

        res.json({
            successful: true,
            data: {
                festivals: Festival.toObjectArray(festivals, {
                    recursive: true
                })
            }
        });
    }
};

let actionTimeSettings = {
    handler(req, res) {
        let schoolId = req.params.schoolId;
        let timeSettings = schoolService.getTimeSettings(schoolId);

        console.log(timeSettings.grades.map(grade => { 
            return {
                name: grade.name,
                checkTimeSet: grade.checkTimeSet.toObject()
            }
        }));
        console.log(timeSettings.checkTimeSet.toObject());
        console.log(timeSettings.festivals.map(festival => festival.toObject()));
        res.json({
            successful: true
        });
    }
};

let actionGetTerms = {
    login:true,
    privileges: [ 'leader' ],
    handler(req, res) {
        let user = req.session.login.user;
        let leader = teacherService.getTeacherByUserId(user.id);
        let school = leader.managedSchool;
        let schoolId = req.params.schoolId;

        if ( school.id != schoolId ) {
            throw errors.NotLeaderOfSchool;
        }

        if ( school.terms == null ) {
            school.terms = [];
        }

        res.json({
            successful: true,
            data: {
                terms: Term.toObjectArray(school.terms, {
                    recursive: true
                })
            }
        });
    }
};

let actionAddTerm = {
    login:true,
    privileges: [ 'leader' ],
    fields: {
        term: {
            name: true,
            startTime: true,
            endTime: true
        }
    },
    handler(req, res) {
        let user = req.session.login.user;
        let leader = teacherService.getTeacherByUserId(user.id);
        let school = leader.managedSchool;
        let schoolId = req.params.schoolId;

        if ( school.id != schoolId ) {
            throw errors.NotLeaderOfSchool;
        }

        let term = schoolService.createTerm(school, req.body.term);

        res.json({
            successful: true,
            data: {
                term: term.toObject({
                    recursive: true
                })
            }
        });
    }
};

let actionUpdateTerm = {
    login:true,
    privileges: [ 'leader' ],
    fields: {
        term: {
            id: true,
            name: true,
            startTime: true,
            endTime: true
        }
    },
    handler(req, res) {
        let user = req.session.login.user;
        let leader = teacherService.getTeacherByUserId(user.id);
        let school = leader.managedSchool;
        let schoolId = req.params.schoolId;

        if ( school.id != schoolId ) {
            throw errors.NotLeaderOfSchool;
        }

        let term = school.terms.find(term => {
            return term.id == req.body.term.id;
        });
        term.name = req.body.term.name;
        term.startTime = req.body.term.startTime;
        term.endTime = req.body.term.endTime;
        schoolService.updateTerm(term);

        res.json({
            successful: true,
            data: {
                term: term.toObject({
                    recursive: true
                })
            }
        });
    }
};

let actionRemoveTerm = {
    login:true,
    privileges: [ 'leader' ],
    fields: {
        term: {
            id: true
        }
    },
    handler(req, res) {
        let user = req.session.login.user;
        let leader = teacherService.getTeacherByUserId(user.id);
        let school = leader.managedSchool;
        let schoolId = req.params.schoolId;

        if ( school.id != schoolId ) {
            throw errors.NotLeaderOfSchool;
        }

        let term = school.terms.find(term => {
            return term.id == req.body.term.id;
        });
        schoolService.removeTerm(school, term);

        res.json({
            successful: true
        });
    }
};

module.exports = {
    actionGetAllSchools,
    actionUpdateFestivals,
    actionTimeSettings,
    actionGetTerms,
    actionAddTerm,
    actionUpdateTerm,
    actionRemoveTerm,
};
